package com.smartlifev30.baiwei.view.banner;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.smartlifev30.R;
import com.smartlifev30.utils.Glide;

import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author tys
 * @date 2018/8/16
 */

public class BannerView extends FrameLayout {
    private BannerCore bannerCore;
    private LinearLayout linearLayout;
    private int id;
    private Timer timer;
    private long DELAY = 4 * 1000;
    private TimerHandle handle;

    public BannerView(Context context) {
        super(context);
    }

    public BannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child) {
        if (bannerCore == null) {
            bannerCore = new BannerCore(getContext()) {
                @Override
                protected void currentBannerImagePosition(int last, int current) {
                    super.currentBannerImagePosition(last, current);
                    ImageView lastDot = linearLayout.findViewWithTag(last);
                    lastDot.setImageResource(R.drawable.guide_point_nor);
                    ImageView dot = linearLayout.findViewWithTag(current);
                    dot.setImageResource(R.drawable.guide_point_selected);
                }
            };
            bannerCore.addView(child);
            super.addView(bannerCore);
            //小圆点
            linearLayout = new LinearLayout(getContext());
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            linearLayout.addView(getDotView(id++, true));
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.END | Gravity.BOTTOM);
            params.setMargins(24, 0, 0, 10);
            super.addView(linearLayout, params);
        } else {
            bannerCore.addView(child);
            linearLayout.addView(getDotView(id++, false));
        }
    }

    public void setOnBannerClickListen(BannerClickListener listen) {
        bannerCore.setBannerClickListen(listen);
    }

    /**
     * 圆点
     *
     * @param id     位置. 用于设置id, 后面根据id设置是选中还是未选中
     * @param select 用於初始化时选中(第一个)
     * @return
     */
    private View getDotView(int id, boolean select) {
        ImageView dot = new ImageView(getContext());
        dot.setTag(id);
        if (select) {
            dot.setImageResource(R.drawable.guide_point_selected);
        } else {
            dot.setImageResource(R.drawable.guide_point_nor);
        }
        dot.setPadding(8, 0, 8, 0);
        return dot;
    }

    /**
     * 设置轮播的图片
     *
     * @param srcIds 图片资源id
     */
    public void setImageResource(int[] srcIds) {
        for (int bannerImg : srcIds) {
            ImageView iv = new ImageView(getContext());
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            Glide.with(this)
                    .load(bannerImg)
                    .fitCenter()
                    .into(iv);
            addView(iv);
        }

    }

    /**
     * 开始轮播
     */
    public void startBanner() {
        if (bannerCore != null) {
            timer = new Timer();
            handle = new TimerHandle(bannerCore);
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    handle.sendEmptyMessage(0);
                }
            }, DELAY, DELAY);
        }
    }

    /**
     * 停止轮播
     */
    public void stopBanner() {
        if (timer != null) {
            timer.cancel();
            timer = null;
            handle.removeCallbacks(null);
            handle = null;
        }
    }

    /**
     * 点击事件
     */
    public interface BannerClickListener {
        void onBannerClick(int index);
    }

    static class TimerHandle extends Handler {
        WeakReference<BannerCore> bannerCoreWeakReference;

        TimerHandle(BannerCore bannerCore) {
            bannerCoreWeakReference = new WeakReference<>(bannerCore);
        }

        @Override
        public void handleMessage(Message msg) {
            BannerCore bannerCore = bannerCoreWeakReference.get();
            if (bannerCore != null) {
                if (bannerCore.isAutoScan) {
                    if (++bannerCore.current >= bannerCore.childCount) {
                        bannerCore.current = 0;
                    }
                    bannerCore.scrollTo(bannerCore.current);
                }
            }
        }
    }

}
